// 每次延迟载入的项数
var lazyLoadItemCount = 2;
// 获取文档高度(包括滚动条的距离)
function getDocumentOffset()
{
    return document.documentElement.clientHeight+window.pageYOffset;
}
// 载入文章列表里的项
function loadArticleItems()
{
    idArray = $.grep(idArray,
        function(value,index){
            if(index < lazyLoadItemCount && value != "undefine")
            {
                $.ajax({
                    type : "GET",
                    contentType : "application/json;charset=utf-8",
                    url : "getarticlelistitems/" + value,
                    dataType : "JSON",
                    success : function(items){
                        if(items != "undefine")
                        {
                            var articleBody = '';
                            articleBody += '<table class="article">';
                            articleBody += '<tr>';
                            articleBody += '<td rowspan="6" class="line">';
                            articleBody += '<div>&nbsp;</div>';
                            articleBody += '</td>';
                            articleBody += '</tr>';
                            articleBody += '<tr>';
                            articleBody += '<td class="articleTitle">';
                            articleBody += '<a href="article/' + items.articleID + '" target="_blank">' + items.articleTitle + '</a>';
                            articleBody += '<span class="publicTime">' + items.publishDate + ' by ' + items.articleAuthor + '</span>';
                            articleBody += '</td>';
                            articleBody += '</tr>';
                            articleBody += '<tr>';
                            articleBody += '<td class="articleAttr">';
                            articleBody += '<span class="articleCategorys">{ <img id="category_loading_' + items.articleID + '" src="theme/images/global/small_loading.gif" /> }</span>';
                            articleBody += '<span class="articleTags">';
                            articleBody += '<img class="tagIcon" src="theme/images/article/tag_icon.png" />';
                            articleBody += '<span class="tags">';
                            articleBody += 'Tag [ <img id="tag_loading_' + items.articleID + '" src="theme/images/global/small_loading.gif" /> ]';
                            articleBody += '</span>';
                            articleBody += '</span>';
                            articleBody += '</td>';
                            articleBody += '</tr>';
                            articleBody += '<tr>';
                            articleBody += '<td class="summary">';
                            articleBody += '<hr />';
                            articleBody += '<p>';
                            articleBody += '<img class="quote" src="theme/images/article/quote.gif" />';
                            articleBody += items.excerpt;
                            articleBody += '</p>';
                            articleBody += '</td>';
                            articleBody += '</tr>';
                            articleBody += '<tr>';
                            articleBody += '<td class="articleInfo">';
                            articleBody += '<span class="commentNum">';
                            articleBody += '<img class="commentIcon" src="theme/images/article/comment_icon.png" />';
                            articleBody += '<span class="comments">' + items.commentNum + '条评论</span>';
                            articleBody += '</span>';
                            articleBody += '<span class="viewNum">';
                            articleBody += '<img class="viewIcon" src="theme/images/article/comment_icon.png" />';
                            articleBody += '<span class="views">' + items.visitNum + '人路过</span>';
                            articleBody += '</span>';
                            articleBody += '<a class="readMore" href="article/' + items.articleID + '" target="_blank">READ MORE</a>';
                            articleBody += '</td>';
                            articleBody += '</tr>';
                            articleBody += '</table>';
    						$("#list").append(articleBody);
                            loadArticleCategory(items.articleID);
                            loadArticleTags(items.articleID);
                        }
                    }
                });
            }
            return index < lazyLoadItemCount;
        },
    true);			
}
// 载入文章分类
function loadArticleCategory(articleId)
{
    $.ajax({
        type : "GET",
        contentType : "application/text;charset=utf-8",
        url : "getarticlecategory/" + articleId,
        dataType : "TEXT",
        success : function(categoryName){
            $('<a href="#">' + categoryName + '</a>').insertAfter("#category_loading_" + articleId);
            $("#category_loading_" + articleId).hide();
        }
    });
}
// 载入文章标签
function loadArticleTags(articleId)
{
    $.ajax({
        type : "GET",
        contentType : "application/json;charset=utf-8",
        url : "getarticletags/" + articleId,
        dataType : "JSON",
        success : function(tagArray){
            var html = '';
            for(var i=0; i < tagArray.length; i++)
            {
                html += '<a href="#">' + tagArray[i] + '</a>,';
            }
            html = html.substr(0,html.length - 1);
            $(html).insertAfter("#tag_loading_" + articleId);
            $("#tag_loading_" + articleId).hide();
        }
    });
}
// 载入评论
function loadComments(articleId)
{
    $.ajax({
        type : "GET",
        contentType : "application/json;charset=utf-8",
        url : "getcomments/" + articleId,
        dataType : "JSON",
        success : function(commentArray){
            if(commentArray != '')
            {
                $(commentArray).each(function(index,value){
                    if(value.Blog_Comment_ParentID == null)
                    {
                        var html = '\
                        <table class="comment" cellpadding="0" cellspacing="0">\
                            <tr>\
                                <!-- 评论背景顶部 -->\
                                <td class="comment_bg_top" colspan="3">&nbsp;</td>\
                            </tr>\
                            <tr>\
                                <!-- 评论背景左侧 -->\
                                <td class="comment_bg_left">&nbsp;</td>\
                                <!-- 评论内容 -->\
                                <td class="comment_content_td">\
                                    <div class="comment_content_container">\
                                        <img class="headImg" src="http://www.gravatar.com/avatar/' + value.Blog_Comment_Email + '" />\
                                        <div class="commentAuthor">' + value.Blog_Comment_Author + '</div>\
                                        <div class="commentPublishTime">' + value.Blog_Comment_PublishTime + '</div>\
                                        <div class="comment_content">\
                                            ' + value.Blog_Comment_Content + '\
                                        </div>\
                                        <a class="replyLink" href="#">回复Reply</a>\
                                        <div class="reply" id="comment_' + value.Blog_Comment_ID + '"></div>\
                                    </div>\
                                </td>\
                                <!-- 评论背景右侧 -->\
                                <td class="comment_bg_right">&nbsp;</td>\
                            </tr>\
                            <tr>\
                                <!-- 评论背景底部 -->\
                                <td class="comment_bg_bottom" colspan="3">&nbsp;</td>\
                            </tr>\
                        </table>';
                        $("#comments").prepend(html);
                    }
                    else
                    {
                        var html = '\
                        <div class="comment_content_container">\
                            <img class="headImg" src="http://www.gravatar.com/avatar/' + value.Blog_Comment_Email + '" />\
                            <div class="commentAuthor">' + value.Blog_Comment_Author + '</div>\
                            <div class="commentPublishTime">' + value.Blog_Comment_PublishTime + '</div>\
                            <div class="comment_content">\
                                ' + value.Blog_Comment_Content + '\
                            </div>\
                            <a class="replyLink" href="#">回复Reply</a>\
                            <div class="reply" id="comment_' + value.Blog_Comment_ID + '"></div>\
                        </div>';
                        $("#comment_" + value.Blog_Comment_ParentID).append(html);
                    }
                });
            }
        }
    });
}
